﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MobileQuiz
{
    public partial class frmMultipleChoiceQuestion : Form
    {
        DataSet dataSet = new DataSet();
        DataTable dataTableQuestion;
        DataTable dataTableAnswers;

        private List<RadioButton> answers = new List<RadioButton>(5);
        private int currentQuestion = 0;
        private int correctAnswer = 0;
        private int numberQuestions = 0;

        private int numberCorrect = 0;

        public frmMultipleChoiceQuestion()
        {
            InitializeComponent();

            // read quiz data file
            dataSet.ReadXml(@"\Program Files\MobileQuiz\quiz.xml");

            // load questions
            dataTableQuestion = dataSet.Tables["question"];
            numberQuestions = dataTableQuestion.Rows.Count;

            // load answers
            dataTableAnswers = dataSet.Tables["answer"];
            
            answers.Add(radioButtonChoice1);
            answers.Add(radioButtonChoice2);
            answers.Add(radioButtonChoice3);
            answers.Add(radioButtonChoice4);
            answers.Add(radioButtonChoice5);
        }

        private void frmMultipleChoiceQuestion_Load(object sender, EventArgs e)
        {
            LoadQuestion();
        }

        private void ClearAnswers()
        {
            labelAlert.Text = string.Empty;

            radioButtonChoice1.Checked = false;
            radioButtonChoice2.Checked = false;
            radioButtonChoice3.Checked = false;
            radioButtonChoice4.Checked = false;
            radioButtonChoice5.Checked = false;
        }

        private void LoadQuestion()
        {
            ClearAnswers();

            int questionId = (int)dataTableQuestion.Rows[currentQuestion]["question_Id"];
            correctAnswer = int.Parse(dataTableQuestion.Rows[currentQuestion]["correct"].ToString());
            webBrowser.DocumentText = dataTableQuestion.Rows[currentQuestion]["text"].ToString();

            DataRow[] dataRowAnswers = dataTableAnswers.Select(string.Format("question_Id = {0}", questionId));

            for (int i = 0; i < dataRowAnswers.Length; i++)
            {
                answers[i].Text = dataRowAnswers[i]["answer_Text"].ToString();
            }
        }

        private void menuItemNext_Click(object sender, EventArgs e)
        {
            bool isAnswered = false;

            isAnswered |= (radioButtonChoice1.Checked);
            isAnswered |= (radioButtonChoice2.Checked);
            isAnswered |= (radioButtonChoice3.Checked);
            isAnswered |= (radioButtonChoice4.Checked);
            isAnswered |= (radioButtonChoice5.Checked);

            if(isAnswered == false)
            {
                labelAlert.ForeColor = Color.Red;
                labelAlert.Text = "You must choose an answer.";
                return;
            }

            int userAnswer = 0;
            for (int i = 0; i < answers.Count; i++)
            {
                if (answers[i].Checked == true)
                    userAnswer = i + 1;
            }

            string message = string.Empty;
            if (userAnswer == correctAnswer)
            {
                numberCorrect++;

                labelAlert.ForeColor = Color.Green;
                labelAlert.Text = "You got this question correct.";
            }
            else
            {
                labelAlert.ForeColor = Color.Red;
                labelAlert.Text = "You got this question incorrect.";
            }

            Application.DoEvents();
            Thread.Sleep(2000);

            currentQuestion++;

            if (currentQuestion >= numberQuestions)
            {
                string endMessage = string.Format("You got {0} out of {1} correct.", numberCorrect, numberQuestions);
                MessageBox.Show(endMessage, "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                this.Close();
            }
            else
            {
                LoadQuestion();
            }
        }
    }
}